home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11768 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  49 lines

  1. Path: news1.erols.com!newsmaster@erols.com
  2. From: ccobb <ccobb@erols.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: extern consts
  5. Date: 16 Mar 1996 03:09:19 GMT
  6. Organization: CSEG, Inc.
  7. Message-ID: <4idbcv$ue2@news7.erols.com>
  8. NNTP-Posting-Host: ccobb.erols.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.22KIT (Windows; U; 16bit)
  13.  
  14. The following code works on one C++ compiler but does not work on two 
  15. others I have tried.  Can anyone give me an authoritive answer as to 
  16. whether this is legal C++?
  17.  
  18. --- xxx.h ---
  19. extern const int MYCONST;
  20. --- xxx.cc ---
  21. #include "xxx.h"
  22. const int MYCONST = 11;
  23. --- yyy.cc ---
  24. #include <iostream.h>
  25. #include "xxx.h"
  26. main()
  27. {
  28.    char myarray[MYCONST];
  29.  
  30.    cout << "MYCONST is " << MYCONST << endl;
  31.    cout << "sizeof myarray[] is " << sizeof(myarray) << endl;
  32.    return 0;
  33. }
  34. --- end ---
  35.  
  36. The whole point here is whether consts can be extern.  Arrays require 
  37. constant expressions.  Techincally, the array size is a constant because 
  38. it is declared const.  However, the value of the constant is set in 
  39. another file.  This means that you could change your constants without 
  40. having to recompile!  If you think the above is trivial you are missing 
  41. the point.  The above is not possible in C nor in two of the three C++ 
  42. compilers I have tried it on.
  43.  
  44. Any comments?
  45.  
  46. Chris Cobb
  47. ccobb@erols.com
  48.  
  49.